<?php

namespace App\Http\Controllers;

use DB;
use Illuminate\Http\Request;
use Validator;

use Ptime3\Functions\JoinData;
use Ptime3\Functions\Queue;

class TeamRecommendController extends Controller
{
    public function index(Request $request){
        $input = $request->all();
        //参数检验
        $validator = Validator::make($input,
            [
                'user_id'     => 'required|min:1',
            ]
        );
        if($validator->fails())
            return $this::jsonResponse(true,$validator->messages(),"paramError");

        $userId   = $input["user_id"];

        //查询
        $teamRecommendResult = DB::table("tm_team_recommend")
            ->select("tm_team_recommend.id","tm_team_recommend.team_name","tm_team.id AS team_id",
                "tm_team.icon","tm_team.description","tm_team.user_count","tm_team.created_at","tm_team.is_verified",
                "tm_team.leader_id")
            ->leftJoin('tm_team', function ($join){
                $join->on('tm_team.source_name', '=', 'tm_team_recommend.source_name')
                    ->where('tm_team.source_id', '=', 'tm_team_recommend.source_id');
            })
            ->where("user_id",$userId)
            ->where("is_joined",false)
            ->get();

        $teamRecommendResult = JoinData::user($teamRecommendResult,"leader");

        return $this->jsonResponse(false,$teamRecommendResult);
    }

    public function store(Request $request){
        $input = $request->all();
        //参数检验
        $validator = Validator::make($input,
            [
                'user_id'     => 'required|integer|min:1',
                'source_name' => 'required|string|min:1',
                'source_id'   => 'required|integer|min:1',
                'team_name'   => 'required|string|min:1',
            ]
        );
        if($validator->fails())
            return $this::jsonResponse(true,$validator->messages(),"paramError");
        
        $userId     = $input["user_id"];
        $sourceName = $input["source_name"];
        $sourceId   = $input["source_id"];
        $teamName   = $input["team_name"];

        $teamRecommendInsertData = [
            "user_id"    => $userId,
            "team_name"  => $teamName,
            "source_name"=> $sourceName,
            "source_id"  => $sourceId
        ];
        $teamRecommendInsertResult = DB::table("tm_team_recommend")->insert($teamRecommendInsertData);

        return $this->jsonResponse(false,$teamRecommendInsertResult);
  	}

    public function agree($recommendId){
        $teamRecommendResult = DB::table("tm_team_recommend")
            ->select("user_id","team_name","source_id","source_name")
            ->where("id",$recommendId)
            ->where("is_joined",false)
            ->first();

        if(empty($teamRecommendResult))
            return $this->jsonResponse(true,[],"推荐不存在。");

        $userId = $teamRecommendResult->user_id;
        $teamId = DB::table("tm_team")
            ->where("source_name",$teamRecommendResult->source_name)
            ->where("source_id",$teamRecommendResult->source_id)
            ->value("id");

        if(empty($teamId)){
            $teamData = [
                "name"       => $teamRecommendResult->team_name,
                "leader_id"  => $userId,
                "is_public"  => true
            ];
            Queue::put("restapi",["post","team","team",$teamData]);
        }else{
            Queue::put("restapi",["post","team","team/$teamId/user/$userId"]);
        }

        $teamRecommendData = [
            "is_joined"=>true,
            "join_time"=>date("Y-m-d H:i:s")
        ];
        DB::table("tm_team_recommend")->where("id",$recommendId)->update($teamRecommendData);
        
        return $this->jsonResponse(false,true);
    }

}
